package com.bamboo.framework.framework.security.service.impl;

import com.bamboo.framework.framework.security.entity.User;
import com.bamboo.framework.framework.security.entity.enums.GenderType;
import com.bamboo.framework.framework.security.mapper.UserMapper;
import com.bamboo.framework.framework.security.service.IUserService;
import com.bamboo.framework.framework.util.BCryptPasswordEncoderUtil;
import com.bamboo.framework.common.format.ReturnMsg;
import com.bamboo.framework.common.enums.Status;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import javax.annotation.Resource;
import jodd.util.StringUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户业务实现类
 * </p>
 *
 * @author 郭晴
 * @since 2018-12-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService,
    UserDetailsService {

  @Resource
  private UserMapper userMapper;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userMapper.selectUserByName(username);
    if (user == null) {
      throw new UsernameNotFoundException("当前用户不存在");
    }
    return user;
  }


  @Override
  public ReturnMsg getPage(Integer current, Integer size, String phone, String nickname,
      String username) {

    if (current < 1 || size < 0) {
      return new ReturnMsg(Status.PARAMETER_NULL, "分页参数错误");
    }
    Page<User> userPage = new Page<>(current, size);
    QueryWrapper<User> user = new QueryWrapper<>();
    if (StringUtil.isNotEmpty(phone)) {
      user.like("phone", phone);
    }
    if (StringUtil.isNotEmpty(nickname)) {
      user.like("nickname", nickname);
    }
    if (StringUtil.isNotEmpty(username)) {
      user.eq("username", username);
    }
    user.groupBy("creat_time");

    return new ReturnMsg(Status.SUCCESS, userMapper.selectPage(userPage, user));
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public ReturnMsg saveUser(User user) {
    System.out.println("UserServiceImpl.saveUser" + user.toString());
    if (StringUtil.isEmpty(user.getUsername()) || StringUtil.isEmpty(user.getNickname())
        || StringUtil.isEmpty(user.getPhone())) {
      return new ReturnMsg(Status.PARAMETER_NULL, "必填字段有空值");
    }

    User username = this.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
    if (user.getId() != null) {
      User byId = this.getById(user.getId());
      if (byId == null) {
        return new ReturnMsg(Status.DATA_DOES_NOT_EXIST, "该用户已不存在");
      }

      if (username != null && StringUtil.isNotEmpty(byId.getUsername()) && !byId.getUsername()
          .equals(username.getUsername())) {
        return new ReturnMsg(Status.DATA_DOES_EXIST, "该用户名已存在，请勿重复添加");
      }
      if ("admin".equals(byId.getUsername()) && StringUtil.isNotEmpty(byId.getUsername()) && !byId
          .getUsername().equals(username.getUsername())) {
        return new ReturnMsg(Status.ERROR, "超级管理员用户账号不能修改");
      }

      user.setUpdateTime(new Date());
    } else {
      if (username != null) {
        return new ReturnMsg(Status.DATA_DOES_EXIST, "该用户名已存在，请勿重复添加");
      }
      user.setCreatTime(new Date());
      user.setUpdateTime(new Date());
      user.setPassword(BCryptPasswordEncoderUtil.getPassword("123456"));
      if (user.getGender() == null) {
        user.setGender(GenderType.male);
      }
    }
    boolean b = this.saveOrUpdate(user);
    if (b) {
      return new ReturnMsg(Status.SUCCESS, "保存成功");
    } else {
      return new ReturnMsg(Status.ERROR, "保存失败");
    }
  }
}
